<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleCategoryMappingsModel;
use App\Models\Article\ArticleModel;
use App\Models\Article\ArticleUserRelationModel;
use App\Service\Article\ArticleService;
use Illuminate\Http\Resources\MissingValue;

class ApiArticleVideoListResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"                  => $this->id,
            "type"                => $this->type,
            "title"               => $this->title,
            "previewUrl"          => json_decode($this->previewUrl, true),
            "coverImgUrl"         => $this->coverImgUrl,
            "description"         => $this->description,
            "supportNum"          => $this->supportNum,
            "collectNum"          => $this->collectNum,
            "forwardNum"          => $this->forwardNum,
            "readNum"             => $this->readNum,
            "showNum"             => $this->showNum,
            "content"             => ArticleService::replaceTranscodingContent(
                $this->content,
                $this->transcodingVideoDetail
            ),
            "formatEffectiveTime" => formatDate($this->effectiveTime),
            "effectiveTime"       => $this->effectiveTime,
            "status"              => $this->status,
            "statusText"          => translateEnumCodeToCn(ArticleModel::STATUS_TEXT, $this->status, "-"),
            "publishType"         => $this->publishType,
            "publishTypeText"     => translateEnumCodeToCn(ArticleModel::PUBLISH_TYPE_TEXT, $this->publishType, "-"),
            "createTime"          => $this->createdAt->timestamp,
            "formatCreateTime"    => formatDate($this->createdAt->timestamp, "-"),
            "sort"                => $this->sort,
            "putTop"              => $this->putTop,
            "isCollect"           => $this->whenLoaded(
                "articleUserRelationRel",
                function () {
                    return (bool)$this->articleUserRelationRel->where("articleId", $this->id)
                                                              ->where("type",
                                                                  ArticleUserRelationModel::TYPE_COLLECT)
                                                              ->where("status",
                                                                  ArticleUserRelationModel::STATUS_SUCCESS)
                                                              ->isNotEmpty();
                },
                false
            ),
            "isSupport"           => $this->whenLoaded(
                "articleUserRelationRel",
                function () {
                    return (bool)$this->articleUserRelationRel->where("articleId", $this->id)
                                                              ->where("type",
                                                                  ArticleUserRelationModel::TYPE_LIKE)
                                                              ->where("status",
                                                                  ArticleUserRelationModel::STATUS_SUCCESS)
                                                              ->isNotEmpty();
                },
                false
            ),
            "shareUrl"            => config('open-shop.m') . "content/detail/" . $this->id,
            "extra"               => $this->extra,
            "categoryDetail"      => $this->whenLoaded("articleCategoryMappingsRel", function () {
                return $this->getCategoryDetail();
            }, new MissingValue())
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @return null
     */
    public function getCategoryDetail()
    {
        if (
            $this->articleCategoryMappingsRel
            &&
            $category = $this->articleCategoryMappingsRel->sortBy("articleCategoryRel.sort")->first()
        ) {
            return $category->articleCategoryRel->toArray();
        }
        {
            return null;
        }
    }
}
